using System.Collections.Generic;
using UnityEngine;

public class ManagerScrollButMap : MonoBehaviour
{
	private List<RoomInfo> roomList = new List<RoomInfo>();

	private List<butScrollRoom> listButton = new List<butScrollRoom>();

	public butScrollRoom exampleBut;

	public GameObject parentButton;

	private UIGrid gridScript;

	private void OnEnable()
	{
		if (controllerConnectPhoton.thisScript != null)
		{
			controllerConnectPhoton.thisScript.OnReceivedRoomListUpdate();
		}
	}

	public void UpdateListRooms()
	{
		if (gridScript == null)
		{
			gridScript = parentButton.GetComponent<UIGrid>();
		}
		roomList = controllerConnectPhoton.thisScript.filteredRoomList;
		SetButtonCount(roomList.Count);
		for (int i = 0; i < listButton.Count; i++)
		{
			butScrollRoom butScrollRoom2 = listButton[i];
			butScrollRoom2.SetInfoRoom(roomList[i]);
		}
		Sort();
		Invoke("Sort", 0.2f);
	}

	private void Sort()
	{
		if (gridScript != null)
		{
			gridScript.repositionNow = true;
		}
	}

	private void SetButtonCount(int needCount)
	{
		if (listButton.Count < needCount)
		{
			for (int i = listButton.Count; i < needCount; i++)
			{
				butScrollRoom item = CreateButton();
				listButton.Add(item);
			}
		}
		else if (listButton.Count > needCount)
		{
			int num = listButton.Count - needCount;
			for (int j = 0; j < num; j++)
			{
				GameObject obj = listButton[listButton.Count - 1].gameObject;
				listButton[listButton.Count - 1] = null;
				listButton.RemoveAt(listButton.Count - 1);
				Object.Destroy(obj);
			}
		}
	}

	private butScrollRoom CreateButton()
	{
		GameObject gameObject = Object.Instantiate(exampleBut.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
		butScrollRoom component = gameObject.GetComponent<butScrollRoom>();
		component.nameRoom = string.Empty;
		component.labelKolPlayers.text = string.Empty;
		component.labelNameRoom.text = "Random Server";
		gameObject.transform.parent = parentButton.transform;
		gameObject.SetActive(true);
		gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		return component;
	}
}
